package com.elitetyc.customview.utils;

import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.elitetyc.customview.R;

/**
 * Created by elitetyc on 2019/4/28.
 * Describe:
 */
public class KeyBoardUtil {
    private KeyboardView keyboardView;
    private Keyboard keyboard;
    private EditText editText1;

    public void showKeyBoard(){
        if (keyboardView.getVisibility()== View.GONE||keyboardView.getVisibility()== View.INVISIBLE){
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    public KeyBoardUtil(KeyboardView keyboardView, EditText editText) {
//        editText.setInputType(InputType.TYPE_NULL);
        this.keyboardView = keyboardView;
        this.editText1 = editText;
        keyboard = new Keyboard(editText.getContext(), R.xml.custom_view_aqjp);
        this.keyboardView.setKeyboard(keyboard);
        this.keyboardView.setPreviewEnabled(false);
        this.keyboardView.setEnabled(true);
        this.keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
            @Override
            public void onPress(int primaryCode) {

            }

            @Override
            public void onRelease(int primaryCode) {

            }

            @Override
            public void onKey(int primaryCode, int[] keyCodes) {

                Editable editable = editText1.getText();
                int start = editText1.getSelectionStart();
                if (primaryCode==Keyboard.KEYCODE_DELETE){
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }else
                editable.insert(start,Character.toString((char) primaryCode));

            }

            @Override
            public void onText(CharSequence text) {

            }

            @Override
            public void swipeLeft() {

            }

            @Override
            public void swipeRight() {

            }

            @Override
            public void swipeDown() {

            }

            @Override
            public void swipeUp() {

            }
        });

    }


}
